package org.lzh.controller;


import org.apache.ibatis.annotations.Param;
import org.lzh.pojo.Problem;
import org.lzh.service.ProblemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.jws.WebParam;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.List;

//题目管理类
@Controller
@RequestMapping("problem")
public class ProblemController {

    @Autowired
    private ProblemService problemService;



    //添加学生用户的弹窗展示页面
    @RequestMapping(value="/showAddProblem")
    public String showAddUser(){
        return "problem/addProblem";
    }


    //    ---------------------------------------------------------------新增操作-----------------------------------------------------------

    //新建问题
    @RequestMapping(value="/insert")
    public String insertProblem(@ModelAttribute Problem problem){
        //int a=problemService.insertProblem(problem);
        Problem p=problemService.findById("2");
        System.out.println("------------------0----=---------------查询出来了-------------------------");
        return "problem/ha";
    }

    //添加问题
    @RequestMapping(value = "/addProblem", method = RequestMethod.POST)
    public String login(HttpServletRequest request,
                        @ModelAttribute Problem problem,//@ModelAttribute这个注解可以将注册时输入的值封装成user对象，很方便
                        Model model             //model可以用model.addAttribute 添加属性，然后在跳转的jsp页面里用，也很好用
    ) throws IOException {
        System.out.println(problem.getId()+problem.getType()+problem.getQuestion()+problem.getOptions()+problem.getAnswer()+problem.getScore());

        if(problem.getId()!=null&&!problem.getType().isEmpty()&&problem.getQuestion()!=null&&problem.getOptions()!=null&&!problem.getAnswer().isEmpty()&&problem.getScore()!=null){
           //判断是否已经存在了这个id
            if(findById(problem.getId())!=null){
                model.addAttribute("id",problem.getId());
                model.addAttribute("tip","学号已存在");
                return "problem/addProblem" ;
            }
            problemService.insertProblem(problem);
            model.addAttribute("tip", "添加成功");
        }else{
            model.addAttribute("tip", "不能为空");
        }
        return "problem/addProblem" ;


    }

    //    ---------------------------------------------------------------删除操作-----------------------------------------------------------

    //删除一个题目
    @RequestMapping(value = "/deleteProblem", method = RequestMethod.POST)
    public @ResponseBody String deleteUser(@Param("id")Integer id) {
        int a= problemService.deleteById(id);
        System.out.println(id);
        if(a > 0){
            return "OK";
        }else{
            return "FAIL";
        }
    }

    //根据ids批量删除用户
    @RequestMapping(value = "/batchDeleteProblem", method = RequestMethod.POST)
    @ResponseBody
    public String batchdeleteById(@Param("ids")String ids){
        for(String id:ids.split(",")){
            Integer id1= Integer.valueOf(id);
            problemService.deleteById(id1);
            System.out.println("-------------------------------------------------------数据库删除成功");
        }
//        model.addAttribute("tip", "删除成功");
        return "删除成功了";
    }


    //    ---------------------------------------------------------------查询操作-----------------------------------------------------------

    //根据id查找问题
    @RequestMapping(value = "/find")
    @ResponseBody
    public Problem findById(String id){
        return problemService.findById(id) ;
    }


    //查询出问题列表，然后跳转到问题管理页面  分页查询（通过数组）
    @RequestMapping(value="/problem/{currPage}/{pageSize}")
    public String user(Model model,
                       @PathVariable("currPage") int currPage,
                       @PathVariable("pageSize") int pageSize,
                       @ModelAttribute Problem problem){

        List<Problem> studentList1=problemService.findByDynamic1(problem);              //动态查询所有的数据,如果没有传user,则会查全部学生
        int length=studentList1.size();
        List<Problem> studentList =problemService.findByDynamic(problem,currPage,pageSize);         //把数据分页切割
        model.addAttribute("studentList",studentList);//分页后的学生数据
        model.addAttribute("length",length);//学生的全部数据的总条数
        int total=length/pageSize+(length%pageSize>0?1:0);            //总页数
        model.addAttribute("total",total);//学生的全部数据的总页数


        model.addAttribute("currPage",currPage);
        model.addAttribute("pageSize",pageSize);
//        model.addAttribute("tip","查询成功");
        model.addAttribute("problem",problem);

        return "problem/problem";
    }

    //根据id查找用户,然后展示在更新操作的表上
    @RequestMapping(value = "/showUpdateProblem")
    public String findById(@Param("id")String id,Model model){
        Problem problem=problemService.findById(id);
        model.addAttribute("problem",problem);
        return "problem/updateProblem";
    }
    //    ---------------------------------------------------------------更新操作-----------------------------------------------------------
    //更新老师或学生个人信息
    @RequestMapping(value = "/updateProblem",method = RequestMethod.POST)
    public String updateById(HttpServletRequest request,
                             @ModelAttribute Problem problem,                          //@ModelAttribute这个注解可以将注册时输入的值封装成user对象，很方便
                             Model model    //model可以用model.addAttribute 添加属性，然后在跳转的jsp页面里用，也很好用
    ) throws IOException {
        System.out.println(problem.getId()+problem.getType()+problem.getQuestion()+problem.getOptions()+problem.getAnswer()+problem.getScore());

        if(problem.getId()!=null&&!problem.getType().isEmpty()&&problem.getQuestion()!=null&&problem.getOptions()!=null&&!problem.getAnswer().isEmpty()&&problem.getScore()!=null){
            problemService.updateById(problem);
            model.addAttribute("tip", "修改成功");
        }else{
            model.addAttribute("tip", "修改失败");
        }

        return "problem/updateProblem";
    }
}
